﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../Scripts/jquery-3.3.1.js"></script>
    <script src="../layui/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style>
        .layui-inline .layui-input {
            height: 30px;
        }
        .layui-form-item .layui-btn+.layui-btn {
            margin-left: 60px;
        }
    </style>
</head>

<body style="margin:20px 20px;">
    <div class="search">
        <div class="layui-inline">
            <input class="layui-input" name="keyword" id="course_id" placeholder="课程号" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input class="layui-input" name="keyword" id="test_question_name" placeholder="试卷名称" autocomplete="off">
        </div>
        <button class="layui-btn layui-btn-normal  layui-btn-sm" id="search_btn" data-type="reload">搜索</button>
    </div>
    
  <div class="layui-btn-container" id="toolbarDemo" style="display:none">
    <button class="layui-btn layui-btn-sm" id="add_btn" lay-event="AddData" >添加</button>
  </div>
    <!-- 表格行操作工具按钮 -->
<div style="display:none" id="barDemo">
        
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </div>
    <div>
        <table class="layui-hide" id="test_question_set" lay-filter="test_question_set"></table>
    </div>
    <div class="layui-tab-item">
        <div id="pageDemo"></div>
    </div>

    <!-- 添加的弹出层 -->
   <div class="AddData" id="AddData" style="display:none; padding: 20px">
        <form class="layui-form" action="" style="margin: 5px 20px;" id="AddDataForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">试题名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="test_question_name" class="layui-input">
                    </div>
                </div>
                 <div class="layui-inline">
                    <label class="layui-form-label">所属课程号：</label>
                    <div class="layui-input-block">
                        <input type="text" autocomplete="on" name="course_id" class="layui-input">
                    </div>
                </div>

            </div>

            <div class="layui-form-item">


                <div class="layui-inline">
                    <label class="layui-form-label">单选题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="radio_questions_sum" value="0" class="layui-input question_sum">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">多选题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="mutil_questions_sum"  value="0" class="layui-input question_sum">
                    </div>
                </div>
            </div>



            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">判断题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="judge_questions_sum" value="0" class="layui-input question_sum">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">解答题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="answer_questions_sum" value="0" class="layui-input question_sum">
                    </div>
                </div>
            </div>
                <div class="layui-inline" style="margin-bottom: 24px;">
                    <label class="layui-form-label">试题难度：</label>
                    <div class="layui-input-inline ">
                        <input type="radio" name="test_difficulty_level" id="Option_A" title="很难">
                        <input type="radio" name="test_difficulty_level" id="Option_B" title="难">
                        <input type="radio" name="test_difficulty_level" id="Option_C" title="中等">
                        <input type="radio" name="test_difficulty_level" checked id="Option_D" title="简单">
                        <div class="layui-form-mid layui-word-aux">系统自动判定，也可修改</div>
                    </div>
                     
                </div>
            <div class="layui-form-item">
               
                <div class="layui-inline">
                    <label class="layui-form-label">总分：</label>
                    <div class="layui-input-block">
                        <input type="text" name="test_score" value="100" class="layui-input">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <!-- 编辑的弹出层 -->
     <div class="UpdateData" id="UpdateData" style="display:none; padding: 20px">
        <form class="layui-form" action="" style="margin: 5px 20px;" id="UpdateDataFrom">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">试题号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="test_question_id" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">试题名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="test_question_name" class="layui-input">
                    </div>
                </div>


            </div>

            <div class="layui-form-item">


                <div class="layui-inline">
                    <label class="layui-form-label">单选题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="radio_questions_sum" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">多选题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="mutil_questions_sum"  class="layui-input ">
                    </div>
                </div>
            </div>



            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">判断题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="judge_questions_sum" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">解答题数目：</label>
                    <div class="layui-input-block">
                        <input type="text" name="answer_questions_sum" class="layui-input">
                    </div>
                </div>
            </div>
                <div class="layui-inline" style="margin-bottom: 24px;">
                    <label class="layui-form-label">试题难度：</label>
                    <div class="layui-input-inline ">
                        <input type="radio" name="test_difficulty_level_1" id="option_A" title="很难">
                        <input type="radio" name="test_difficulty_level_1" id="option_B" title="难">
                        <input type="radio" name="test_difficulty_level_1" id="option_C" title="中等">
                        <input type="radio" name="test_difficulty_level_1" checked id="option_D" title="简单">
                    </div>
                    <div class="layui-form-mid layui-word-aux">系统自动判定，也可修改</div>
                </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">所属课程号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="course_id" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">总分：</label>
                    <div class="layui-input-block">
                        <input type="text" name="test_score" class="layui-input">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <script>
        
    </script>

    <script>
        layui.use(['table', 'laypage', 'layer', 'form', 'upload','jquery'], function ()
        {
            var table = layui.table,
                laypage = layui.laypage,
                form = layui.form,
                upload = layui.upload,
                $ = layui.jquery;

            //获取试卷难度等级
            $(".question_sum").change(function ()
            {
                //判断等级
                var r = /^[0-9]{1,2}$/;
                if (!r.test($(this).val()))
                {
                    layer.alert("请输入小于100的数字", { icon: 2, offset: '100px' });
                    $(this).val("0");
                    return;
                }
                var radio = $("input[name='radio_questions_sum']").val();
                var mutil = $("input[name='mutil_questions_sum']").val();
                var judge = $("input[name='judge_questions_sum']").val();
                var answer = $("input[name='answer_questions_sum']").val();
                var i = -1;
                if (Number(answer) != 0)
                {
                    if (Number(radio) / Number(answer) > 10)
                    {
                        i = 3;
                    }
                    else if(Number(radio) / Number(answer) > 7)
                    {
                        i = 2;
                    }
                    else if (Number(radio) / Number(answer) > 5)
                    {
                        i = 1;
                    }
                    else
                    {
                        i = 0;
                    }
                }
                else
                {
                    i =3;
                }
                var items = $("input[type='radio'][name='test_difficulty_level']");
                $(items[i]).next().click();
                
               
            });
            $("input[name='course_id']").change(function ()
            {
                var r = /^[0-9]{9}$/;
                if (!r.test($(this).val()))
                {
                    layer.alert("课程号是9位数字", { icon: 2, offset: '100px' });
                    $(this).val("0");
                    return;
                }
            });
            var ins1 = table.render({ //相当于ajax
                elem: '#test_question_set'
                , url: 'testQuestionManage.aspx'
                , id: 'test_question_set1' //重载时使用
                , title: '试题基本信息表'
                , height: 520
                , page: true
                , limits: [3, 5, 10]  //一页选择显示3,5或10条数据
                , limit: 10  //一页显示10条数据
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
               , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'print', {
                    title: '打印'
                  , layEvent: 'export'
                  , icon: 'layui-icon-export'
                }]
                , cols: [[
                    { field: 'test_question_id', width: 200, title: '试题号', sort: true }
                    , { field: 'test_question_name', width: 100, title: '试题姓名' }
                     , { field: 'course_id', width: 120, title: '所属课程号' }
                       , { field: 'radio_questions_sum', width: 120, title: '单选题数目' }
                       , { field: 'mutil_questions_sum', width: 140, title: '多选题数目' }
                               , { field: 'judge_questions_sum', width: 120, title: '判断题数目' }
                               , { field: 'answer_questions_sum', width: 120, title: '解答题数目' }
                       , { field: 'test_difficulty_level', width: 240, title: '试题难度(A很难，B难,C中等,D简单)' }
                       , { field: 'test_score', width: 100, title: '总分' }
                    , { fixed: 'right', width: 165, align: 'center', title: '操作', toolbar: '#barDemo' }//添加列工具条
                ]]
                , parseData: function (res)
                { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                    console.log(JSON.stringify(res));
                    console.log(JSON.stringify(res.code));
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data //解析数据列表
                    };
                }
                , error: function (xhr, state, errorThrown)
                {
                    requesFail(xhr);
                }
            })
            //监听行工具事件
            table.on('tool(test_question_set)', function (obj)
            { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值

                //console.log(data);

                if (layEvent === 'del')
                {
                    layer.confirm('真的删除行么', function (index)
                    {
                        layer.close(index);
                        //向服务端发送删除指令
                        delStudentInfo(data);
                    });
                } else if (layEvent === 'edit')
                {
                    //layer.msg('编辑操作');
                    //页面层
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['750px', '500px'], //宽高
                        offset: '10px',
                        content: $("#UpdateData"),
                        btn: ['保存更改']
                        , success: function (data)
                        {
                            getData();//填充数据
                            $("#UpdateDataFrom input[name='test_score']").attr("ReadOnly", true);//不可更改
                        }
                        , yes: function (index, layero)
                        {
                            updateUserInfo();
                            //return false;
                        }
                    });
                }
                //对表单数据赋值
                function getData()
                {
                    var from = $("#UpdateDataFrom .layui-input");
                    for (var i = 0; i < from.length; i++)
                    {
                        
                        var key = from[i].name;
                        var value = data[key];
                        from[i].value = value;
                    }
                    if (data["test_difficulty_level"] == "A")
                    {
                        $("#option_A").prop('checked', true);
                    }
                    else if(data["test_difficulty_level"] == "B")
                    {
                        $("#option_B").prop({ "checked": true });
                    }
                    else if (data["test_difficulty_level"] == "C")
                    {
                        $("#option_C").prop({ "checked": true });
                    }
                    else
                    {
                        $("#option_D").prop({ "checked": true });
                    }
                    
                    form.render();
                    
                    


                }
                function delStudentInfo(data)
                {
                    $.ajax({
                        url: '../operate/operateTestQuestion.aspx',
                        datatype: 'text',
                        data: { 'operate': layEvent, 'table': 'tb_test_questions', 'test_question_id': data.test_question_id },
                        success: function (res)
                        {
                            if (res > 0)
                            {
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.msg('删除成功');
                                $(".layui-laypage-btn").click();//刷新当前分页数据
                            }
                            else
                            {
                                layer.msg('删除失败，不存在该试题，请刷新');
                            }
                        },
                        error: function (xhr, textStatus, errorThrown)
                        {
                            alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                            //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                        }
                    });
                }
            });
            function updateStudentInfo(para)
            {
                $.ajax({
                    url: '../operate/operateUserInfo.aspx',
                    datatype: 'text',
                    data: para,
                    success: function (res)
                    {
                        if (res > 0)
                        {
                            layer.msg('更改成功');
                            $(".layui-laypage-btn").click();//刷新当前分页数据
                        }
                        else
                        {
                            layer.msg('更改失败，数据不对');
                        }
                    },
                    error: function (xhr, textStatus, errorThrown)
                    {
                        alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                        //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                    }
                });
            }
            //头工具栏事件
            table.on('toolbar(test_question_set)', function (obj)
            {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event)
                {
                    case 'AddData':
                        //页面层
                        layer.open({
                            type: 1,
                            title: '添加用户',
                            skin: 'layui-layer-rim', //加上边框
                            area: ['750px', '500px'], //宽高
                            offset: '10px',
                            content: $("#AddData"),
                            btn: ['保存']
                            , yes: function (index, layero)
                            {
                                addUserInfo();
                                //return false;
                            }


                        });
                        break;


                        //自定义头工具栏右侧图标 - 导出
                    case 'export':
                        layer.confirm('是否导出数据！', {
                            btn: ['确定', '取消'] //可以无限个按钮
                            , btn2: function (index, layero)
                            {

                                return true;//按钮【按钮二】的回调
                            }
                        }, function (index)
                        {

                            layer.close(index);//按钮【按钮一】的回调
                            exportFile();
                        });
                        break;
                };

            });
            function exportFile()
            {
                var course_id = $('#course_id').val();
                var test_question_name = $('#test_question_name').val();
                var para = {};
                para.course_id = course_id;
                para.test_question_name = test_question_name;
                $.ajax({
                    url: '../export/testQuestionsExport.aspx',
                    methods: 'post',
                    dataType: 'json',
                    data: para,
                    success: function (res)
                    {
                        table.exportFile(ins1.config.id, res.data);
                    },
                    error: function (res)
                    {
                        alert("导出失败");
                    }
                });

            }
            $('#search_btn').click(function ()
            {
                var course_id = $('#course_id').val();
                var test_question_name = $('#test_question_name').val();
                var para = {};
                para.course_id = course_id;
                para.test_question_name = test_question_name;
       
                table.reload('test_question_set1', {
                    url: 'testQuestionManage.aspx'
                    , methods: "post"
                    , where: para
                    , page: {
                        curr: 1
                    }
                });
            });
            $('#save_btn').click(function ()
            {
                //获取表单所有值
                var from = $("#UpdateDataFrom .layui-input");
                var para = {};
                para["operate"] = "update";
                para["table"] = "tb_test_question";
                for (var i = 0; i < from.length; i++)
                {
                    var key = from[i].name;
                    para[key] = from[i].value;
                }
                console.log("保存：" + para);
                updateStudentInfo(para);
            });
            function updateUserInfo()
            {

                //获取表单所有值
                var from = $("#UpdateDataFrom .layui-input");
                var para = {};
                para["operate"] = "update";
                para["table"] = "tb_test_questions";
                for (var i = 0; i < from.length; i++)
                {
                    var key = from[i].name;
                    para[key] = from[i].value;
                }
                var level = $("input[type='radio'][name='test_difficulty_level_1']:checked").attr('id');
                para["test_difficulty_level"] = level.toString().substring(level.length - 1, level.length);
                $.ajax({
                    url: '../operate/operateTestQuestion.aspx',
                    datatype: 'text',
                    data: para,
                    success: function (res)
                    {
                        if (Number(res) > 0)
                        {
                            layer.msg('修改成功');
                        }
                        else if (Number(res) == 0)
                        {
                            layer.msg('修改失败，数据不对');
                        }
                        
                    },
                    error: function (xhr, textStatus, errorThrown)
                    {
                        alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                        //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                    }
                });
            }
            function addUserInfo()
            {
                //保存按钮
                //获取所有输入值
                var items = $('#AddDataForm .layui-input');
                var length = items.length;

                var para = {};
                para["operate"] = "add";
                para["table"] = "tb_test_questions";
                for (var i = 0; i < length; i++)
                {
                    var item = items[i];
                    var t = item.type;
                    para[item.name] = item.value;
                }
                var level = $("input[type='radio'][name='test_difficulty_level']:checked").attr('id');
                para["test_difficulty_level"] = level.toString().substring(level.length - 1, level.length);

                //上传到后台
                $.ajax({
                    url: '../operate/operateTestQuestion.aspx',
                    datatype: 'text',
                    methed: 'post',
                    data: para,
                    success: function (res)
                    {
                        if (res > 0)
                            alert("添加成功");
                        else if (res == -2)
                        {
                            layer.alert("该课程不存在", { icon: 2, offset: '100px' });
                        } else
                        {
                            alert("添加失败，数据不规范");
                        }
                    }
                });

                

            }




        });


        

    </script>
    
</body>

</html>